<?php

namespace App\Http\Requests\Admin\CollOption;

use App\Http\Requests\Request;
use App\Models\Common\CollOption;
use Validator;

class CreateRequest extends Request{
    public function authorize(){
        return true;
    }

    public function rules(){
        $input = $this->all();
        //添加编码排序判断
        Validator::extend('check_opt_sort', function($attribute, $value, $parameters){
            if($value != 0){
                $CollOptionSort = CollOption::where('sign', $this->sign)->pluck('opt_sort')->toArray();
                if(in_array($value, $CollOptionSort)){
                    return false;
                }else{
                    return true;
                }
            }else{
                return true;
            }
        });
        return [
            'opt_name' => 'required',
            'sign' => 'required|sign_match:' . $input['opt_id'],
            'opt_id' => 'required|integer',
            'opt_sort' => 'check_opt_sort'
        ];
    }

    public function messages(){
        return [
            'opt_name.required' => '分类选项名称必须填写',
            'sign.required' => '分类选项标识必须填写',
            'sign.sign_match' => '当前分类选项编码已经存在',
            'opt_id.required' => '分类选项编码必须填写',
            'opt_id.integer' => '分类选项编码必须是一个数字',
            'opt_sort.check_opt_sort' => '分类选项排序已经存在'
        ];
    }
}
